package whu.edu.demo.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
public class JwtTokenUtil {
    public static final long JWT_TOKEN_VALIDITY = 5*60*60*1000;

    @Value("${jwt.secret}")
    private String secret;


    public Claims getClaimFromToken(String token){
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
    }

    public String generateToken(UserDetails userDetails){
        Map<String,Object> claims = new HashMap<>();
        return Jwts.builder().setClaims(claims).setSubject(userDetails.getUsername()).setIssuedAt(new Date(System.currentTimeMillis()))
                .setExpiration(new Date(System.currentTimeMillis()+JWT_TOKEN_VALIDITY))
                .signWith(SignatureAlgorithm.HS512,secret).compact();
    }

    public Boolean validateToken(String token,UserDetails userDetails){
        return validateClaim(getClaimFromToken(token),userDetails)&&canTokenBeRefreshed(token);
    }

    public boolean validateClaim(Claims claim,UserDetails userDetails){
        return userDetails != null &&
                claim.getSubject().equals(userDetails.getUsername());
    }

    public Boolean canTokenBeRefreshed(String token){
        return (!isTokenExpired(token)||ignoreTokenExpiration(token));
    }

    public Boolean isTokenExpired(String token){
        Claims claim = getClaimFromToken(token);
        return claim.getExpiration().before(new Date());
    }

    public Boolean ignoreTokenExpiration(String token){
        return false;
    }
}